<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/2/8
 * Time: 15:06
 */
namespace app\admin\controller;
use app\admin\controller\Auto;
use think\Request;
use think\Db;
class User extends Auto{

    public function index(){
//        var_dump($this->emsconfig);
        $result  = Db::table('organization')
            ->where('org_parent_code',session('user.org_code'))
            ->select();
        $this->assign('org_list',$result);
        return $this->fetch();
    }
//    用户添加
    public function useradd(){
        $Request = Request::instance();
        $param = $Request->param();
        $user_guid = $this::create_guid();
        $data = [
            'acc_name'=>$param['acc_name'],
            'acc_pwd'=>md5($param['acc_pwd']),
            'org_code'=>$param['org_code'],
            'user_role'=>$param['user_role'],
            'mark'=>1,
            'create_date'=>date('Y-m-d H:i:s',time()),
            'user_email'=>$param['user_email'],
            'user_guid'=>substr($user_guid,1,-1),
            ];
        if($param['org_code'] == session('user.org_code') && session('user.user_role')==1){
            $data['user_role'] = $param['user_role']-1;
        }
//        echo json_encode($data);exit();
        $result  = Db::table('user_acc')->insert($data);
        if($result){
            $resultId = Db::table('user_acc')->getLastInsID();
            return $resultId;
        }else{//添加失败
            return 0;
        }

    }
    //    删除用户
    public function userdel(){
        $Request = Request::instance();
        $param = $Request->param();
        if($param['ID'] == 1){
            return "error";
        }
        $ID = $param['ID'];
        $result  = Db::table('user_acc')->delete($ID);
        echo $result;
    }
    //    修改用户
    public function userupd(){
        $Request = Request::instance();
        $param = $Request->param();
        $acc_pwd = $param['acc_pwd'];
        $acc_name = $param['acc_name'];
        $data = [
            'acc_pwd' =>md5($acc_pwd),
        ];
        try{
            $result  = Db::table('user_acc')->where('acc_name',$acc_name)->update($data);
            echo $result;
        } catch (Exception $e){
//            echo $e->getMessage();
            echo '服务器内部错误';
        };
    }
//    用户查询
    public function userquery(){
        $result  = Db::table('user_acc')
            ->where('user_role',session('user.user_role')+1)
//            ->where('org_code',session('user.org_code'))
            ->select();
        $dataarr = [
            'data'=>$result
        ];
        echo json_encode($dataarr);
    }

    static function create_guid(){
        if (function_exists('com_create_guid')) {
            return com_create_guid();
        } else {
            mt_srand((double)microtime()*10000);
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);
            $uuid   = chr(123)
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);
            return $uuid;
        }
    }


}